package com.yphone.ui.pay.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zzkj.yunphone.R;
import com.yphone.ui.event.MyEvent;
import com.yphone.ui.pay.bean.PayInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class PayItem extends LinearLayout {

    private TextView tv_pay_type;
    private TextView tv_price;
    private TextView tv_src_price;
    private TextView tv_yy;
    private TextView tv_des;
    private PayInfo info;
    private View root;

    public PayItem(Context context) {
        this(context, null);
    }

    public PayItem(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PayItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        EventBus.getDefault().register(this);
        root = View.inflate(getContext(), R.layout.item_pay, this);
        tv_pay_type = findViewById(R.id.tv_pay_type);
        tv_price = findViewById(R.id.tv_price);
        tv_yy = findViewById(R.id.tv_price);
        tv_src_price = findViewById(R.id.tv_src_price);
        tv_src_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        tv_des = findViewById(R.id.tv_des);
    }

    public void setData(PayInfo info) {
        this.info = info;
        tv_pay_type.setText(info.name);
        tv_price.setText(info.price + "");
        tv_src_price.setText(info.old_price + "");
        tv_des.setText(info.remark);
    }

    public void setChecked(boolean isCheck) {
        if (isCheck) {
            setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.btn_common_pay_item_blue));
            tv_pay_type.setTextColor(getContext().getResources().getColor(R.color.white));
            tv_price.setTextColor(getContext().getResources().getColor(R.color.white));
            tv_yy.setTextColor(getContext().getResources().getColor(R.color.white));
            tv_src_price.setTextColor(getContext().getResources().getColor(R.color.white));
            tv_des.setTextColor(getContext().getResources().getColor(R.color.blue_288bff));
            tv_des.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.item_pay_ext_while));
        } else {
            setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.btn_common_pay_item));
            tv_pay_type.setTextColor(Color.parseColor("#292929"));
            tv_price.setTextColor(Color.parseColor("#292929"));
            tv_yy.setTextColor(Color.parseColor("#292929"));
            tv_src_price.setTextColor(Color.parseColor("#292929"));
            tv_des.setTextColor(getContext().getResources().getColor(R.color.white));
            tv_des.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.item_pay_ext_blue));
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void handle(MyEvent.PayItemCheckEvent event) {
        if (info.id == event.payInfo.id) {
            setChecked(true);
        } else {
            setChecked(false);
        }
    }


}
